﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI;

namespace DR5.SPExtend.Controls
{
    class SPXHyperLinkField : SPBoundField
    {
        public string[] DataFieldsForNavigateUrl { get; set; }
        public string[] DataFieldsForClickString { get; set; }
        public string NavigateUrl { get; set; }
        public string Text { get; set; }
        public string OnClickString { get; set; }

        public override void InitializeCell(System.Web.UI.WebControls.DataControlFieldCell cell, System.Web.UI.WebControls.DataControlCellType cellType, System.Web.UI.WebControls.DataControlRowState rowState, int rowIndex)
        {
            try
            {
                base.InitializeCell(cell, cellType, rowState, rowIndex);
                switch (cellType)
                {
                    case DataControlCellType.DataCell:
                        this.InitializeDataCell(cell, rowState);
                        break;
                    case DataControlCellType.Footer:
                        this.InitializeFooterCell(cell, rowState);
                        break;
                    case DataControlCellType.Header:
                        this.InitializeHeaderCell(cell, rowState);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void InitializeHeaderCell(DataControlFieldCell cell, DataControlRowState rowState)
        {
            System.Web.UI.WebControls.Label lbl = new System.Web.UI.WebControls.Label();
            lbl.Text = this.HeaderText;
            cell.Controls.Add(lbl);
        }

        private void InitializeFooterCell(DataControlFieldCell cell, DataControlRowState rowState)
        {
            System.Web.UI.WebControls.Label lbl = new System.Web.UI.WebControls.Label();
            lbl.Text = this.HeaderText;
            cell.Controls.Add(lbl);
        }

        private void InitializeDataCell(DataControlFieldCell cell, DataControlRowState rowState)
        {
            HyperLink link = new HyperLink();
            link.DataBinding += new EventHandler(link_DataBinding);
            cell.Controls.Add(link);
        }

        void link_DataBinding(object sender, EventArgs e)
        {
            try
            {

                HyperLink link = sender as HyperLink;

                object dataItem = DataBinder.GetDataItem(link.Parent.Parent);
                DataRow dr = (DataBinder.GetDataItem(link.Parent.Parent) as System.Data.DataRowView).Row;

                if (DataFieldsForNavigateUrl != null)
                {
                    if (DataFieldsForNavigateUrl.Length != 0)
                    {
                        string[] dataValueForNavigateUrl = GetValues(dr, DataFieldsForNavigateUrl);
                        link.NavigateUrl = string.Format(NavigateUrl, dataValueForNavigateUrl);
                    }
                    else
                    {
                        link.NavigateUrl = NavigateUrl;
                    }
                }

                if (DataFieldsForClickString != null)
                {
                    if (DataFieldsForClickString.Length != 0)
                    {
                        string[] dataValueForClickString = GetValues(dr, DataFieldsForClickString);
                        link.Attributes.Add("onclick", string.Format(OnClickString, dataValueForClickString));
                    }
                    else
                    {
                        link.Attributes.Add("onclick", OnClickString);
                    }
                }

                link.Text = Text;
            }
            catch
            {

            }
        }

        private string[] GetValues(DataRow dr, string[] datafields)
        {
            string[] dataValue = new string[datafields.Length];
            int i = 0;
            foreach (string s in datafields)
            {
                try
                {
                    dataValue[i] = dr[s].ToString();
                }
                catch { }
                i++;
            }
            return dataValue;
        }
    }
}
